<?php

error_reporting(E_ALL ^E_NOTICE);
define('IN_PHPSCUP','1');
require_once "zh-cn.lang.php";
define('IN_SCUP',true);
define('ROOT_PATH', dirname(dirname(__FILE__)));
define('PHPSCUP_EDITION','File Edition');
define('PHPSCUP_VERSION','1.4.0');
define('PHPSCUP_RELEASE','20100813');
define('LANG','zh-cn');

define('NOW_YEAR','2010');
$charset = 'utf8';
$total_step = 8;
ob_start();
if(file_exists(ROOT_PATH."/data/install.lock"))
{
	
	exit($lang['install_exsit']);
}

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') 
{
	define('LF',"\r\n");
} else {
	define('LF',"\n");
}




function stripslashes_array(&$array) 
{
	if (is_array($array)) 
	{
		foreach ($array as $k => $v) 
		{
			$array[$k] = stripslashes_array($v);
		}
	} else if (is_string($array)) 
	{
		$array = stripslashes($array);
	}
	return $array;
}

if (@get_magic_quotes_gpc()) 
{
    $_GET = stripslashes_array($_GET);
    $_POST = stripslashes_array($_POST);
}

@set_magic_quotes_runtime(0);

$step = $_GET['step'] ? (int)$_GET['step'] : (int)$_POST['step'];
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$config_file = ROOT_PATH.'/data/db.config.php';

$sqlfile = ROOT_PATH.'/install/install.sql';
if(!file_exists($sqlfile) || !is_readable($sqlfile)) 
{
	exit($lang['db_file_not_exists']);
}
$fp = fopen($sqlfile, 'rb');
$sql = fread($fp, filesize($sqlfile));
fclose($fp);

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$charset?>" />
<title>PHPSCUP <?php echo PHPSCUP_VERSION;?> <?php echo $lang['install_guide'] ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body style="margin-top:5px">
<div id="main">
<?php 

if(!$step || $step ==3 || $step ==4 || $step ==5 || $step ==7)
{

?>
  <form method="post" name="myform" action="<?php echo $php_self;?>" onsubmit="return chksubmit(this);">
  
<?php

}
else
{

?>  
  <form method="post" name="myform" action="<?php echo $php_self;?>">
<?php

}

?>
    <p class="title">PHPSCUP <?php echo PHPSCUP_VERSION;?> <span class="txtgreen">[<?php echo strtoupper($charset); ?>]</span> <?php echo $lang['install_guide'] ?></p>

<?php

if(!$step)
{

?>
<p>
	<li><?php echo $lang['lc_1'] ?></li>

</p>
<p>&nbsp;</p>
<p align="center"><?php echo $lang['welcome_msg']?></p>

<p style="color:#F00; font-weight:bold" align="center">为了能正常运行程序，您的php版本要低于6.0</p>

<p>&nbsp;</p>

<p align="right">
<input type="hidden" name="step" value="1" />
<input class="formbutton" type="submit" value="<?php echo $lang['next_step'] ?>" />
</p>

<?php

}
elseif($step == 1)
{

?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['user_license'] ?></p>
	<p align="center"><textarea rows="50" cols="100" readonly="readonly"><?php echo $lang['license_content'] ?></textarea>
	</p>

    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" type="submit" value="<?php echo $lang['agree'] ?>" />&nbsp;&nbsp;<input class="formbutton" type="button" value="<?php echo $lang['disagree'] ?>" onclick="javascript:window.close();" />
    </p>
<?php
 
}
else if($step ==2)
{
	$short_open_tag = @ini_get('short_open_tag');
	$system_dir = ROOT_PATH.'/data';
	$filestores_dir = ROOT_PATH.'/upfiles';
	$template_dir = ROOT_PATH.'/temp';
	$plugins_dir = ROOT_PATH.'/languages';
	$chk_system_dir = is_writable($system_dir) ? 1 : 0;
	$chk_filestores_dir = is_writable($filestores_dir) ? 1 : 0;
	$chk_template_dir = is_writable($template_dir) ? 1 : 0;
	$chk_plugins_dir = is_writable($plugins_dir) ? 1 : 0;
	if($short_open_tag && $chk_system_dir && $chk_filestores_dir && $chk_template_dir && $chk_plugins_dir)
	{
		$install_halt =0;
	}else
	{
		$install_halt =1;
	}
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['install_note'] ?></p>
    <p><?php echo $lang['welcome_to_use'] ?> PHPSCUP <?php echo PHPSCUP_VERSION;?><?php echo $lang['install_settings'] ?>: </p>
    <ul>
      <li><?php echo $lang['mysql_server_or_ip'] ?> </li>
      <li><?php echo $lang['mysql_username_or_password'] ?> </li>
      <li><?php echo $lang['mysql_database_name'] ?> </li>
      <li><?php echo $system_dir ?> <?php echo $lang['check_dir1'] ?></li>
	  <li><?php echo $system_dir ?> <?php echo $lang['check_dir2'] ?> <?php output_txt($chk_system_dir,1); ?></li>
      <li><?php echo $filestores_dir ?> <?php echo $lang['check_dir1'] ?></li>
	  <li><?php echo $filestores_dir ?> <?php echo $lang['check_dir2'] ?><?php output_txt($chk_filestores_dir,1); ?></li>
      <li><?php echo $template_dir ?> <?php echo $lang['check_dir1'] ?></li>
	  <li><?php echo $template_dir ?> <?php echo $lang['check_dir2'] ?><?php output_txt($chk_template_dir,1); ?></li>
      <li><?php echo $plugins_dir ?> <?php echo $lang['check_dir1'] ?></li>
	  <li><?php echo $plugins_dir ?> <?php echo $lang['check_dir2'] ?><?php output_txt($chk_plugins_dir,1); ?></li>
    </ul>
    <p><?php echo $lang['database_tips_info'] ?></p>
	<?php 
	if($short_open_tag !=1)
	{
	?>
		<p class="txtgreen"><b><?php echo $lang['php_env_setting'] ?></b><?php echo $lang['short_open_tag'] ?></p>
	<?php 
	}
	?>
	<?php if(!$install_halt){?>

    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" type="submit" value="<?php echo $lang['next_step'] ?>" />
    </p>
	<?php }?>
<?php

} 
elseif ($step == 3) 
{

	$fp = fopen($config_file,'w'); 
	if (!$fp) 
	{ 
		exit("Can not open file <b>$config_file</b> .");
	} 
	@fclose($fp);
	$exist_error = false;
	$write_error = false;
	if (file_exists($config_file)) 
	{
		$fileexists = output_txt(1, 0);
	} else 
	{
		$fileexists = output_txt(0, 0);
		$exist_error = true;
	}
	if (is_writable($config_file)) 
	{
		$filewriteable = output_txt(1, 0);
	} else 
	{
		$filewriteable = output_txt(0, 0);
		$write_error = true;
	}
	if ($exist_error) 
	{
		$config_info = $config_file.' '.$lang['config_file_not_exists'];
	} elseif($write_error) 
	{
		$config_info = $lang['config_file_cant_write'];
	}
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['database_setting'] ?></p>
    <p><?php echo $config_file;?> <?php echo $lang['file_exists'] ?> <?php echo $fileexists;?></p>
    <p><?php echo $config_file;?> <?php echo $lang['file_writeable'] ?> <?php echo $filewriteable;?></p>
<?php
	if ($config_info) 
	{
?>
    <p><?php echo $config_info;?></p>

    <p align="right">
      <input class="formbutton" type="button" value="<?php echo $lang['prev_step'] ?>" onclick="history.back(1)" />
    </p>
<?php
	} 
	else 
	{
?>

    <table width="100%" border="0" cellspacing="0" cellpadding="4">
      <tr>
        <td colspan="2" class="bold"><?php echo $lang['base_info'] ?></td>
      </tr>
      <tr>
        <td width="30%"><?php echo $lang['dbhost'] ?>:</td>
        <td><input type="text" value="localhost" name="dbhost" class="formfield" style="width:150px"> <?php echo $lang['dbhost_tips'] ?></td>
      </tr>
      <tr>
        <td><?php echo $lang['dbuser'] ?>:</td>
        <td><input type="text" value="root" name="dbuser" class="formfield" style="width:150px" ></td>
      </tr>
      <tr>
        <td><?php echo $lang['dbpasswd'] ?>:</td>
        <td><input type="password" value="" name="dbpasswd" class="formfield" style="width:150px" ></td>
      </tr>
      <tr>
        <td><?php echo $lang['dbprefix'] ?>:</td>
        <td><input type="text" value="scup_" name="dbprefix" class="formfield" style="width:150px"  ></td>
      </tr>      
      
    </table>
	<script language="javascript">
	document.myform.dbhost.focus();
	function chksubmit(o){
		if(o.dbhost.value == ""){
			alert("<?php echo $lang['js_dbhost'] ?>");
			o.dbhost.focus();
			return false;
		}
		if(o.dbuser.value == ""){
			alert("<?php echo $lang['js_dbuser'] ?>");
			o.dbuser.focus();
			return false;
		}

		o.s1.disabled = true;	
		return true;
	}
	</script>
    <p>&nbsp;</p>
    <p><?php echo $lang['database_tips_info'] ?></p>

    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" name="s1" type="submit" value="<?php echo $lang['next_step'] ?>" />
    </p>
<?php 
	}	
} 
elseif ($step == 4) 
{
	if(trim($_POST['dbhost']) == "" || trim($_POST['dbuser']) == "")
	{

?>
    <p><?php echo $lang['fill_all_blank'] ?></p>

    <p align="right">
      <input class="formbutton" type="button" value="<?php echo $lang['prev_step'] ?>" onclick="history.back(1)" />
    </p>
<?php
	} 
	else 
	{
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['install_database'] ?></p>
<?php	
		$dbhost = trim($_POST['dbhost']);
		$dbuser = trim($_POST['dbuser']);
		$dbpasswd = trim($_POST['dbpasswd']);
		$dbprefix = trim($_POST['dbprefix']);

		
		$_l = mysql_connect($dbhost,$dbuser,$dbpasswd) or die($lang['could_not_connect'] . mysql_error());

		$q = mysql_query("show databases;") or die($lang['invalid_query'] . mysql_error());
		$db_list = array();
		while($rs = mysql_fetch_array($q)){
			$db_list[] = $rs;
		}
		unset($rs);
		mysql_close($_l);
?>
  
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
      <tr>
        <td colspan="2" class="bold"><?php echo $lang['database_info'] ?></td>
      </tr>
	  <tr>
	  	<td colspan="2"><span class="txtred"><?php echo $lang['fill_db_tips'] ?></span></td>
	  </tr>
      <tr>
        <td width="20%"><?php echo $lang['dbname'] ?>:</td>
        <td><input type="radio" id="rd1" name="rd" onclick="chg_db(1);" checked="checked" />
		<input type="text" value="scup" name="dbname1" id="dbname1" class="formfield" style="width:150px" onclick="this.value='';"></td>
      </tr>
	  <tr>
	  	<td></td>
		<td><input type="radio" id="rd2" name="rd" onclick="chg_db(2);" />
		<select name="dbname2" id="dbname2">
		<option value="0"><?php echo $lang['please_select'] ?></option>
		<?php
		sort($db_list);
		foreach($db_list as $k => $v)
		{
			echo '<option value="'.$db_list[$k]['Database'].'">'.$db_list[$k]['Database'].'</option>';
		}
		?>
		</select>
		</td>
	  </tr>
    </table>
	<br />

    <p align="right">
	  <input type="hidden" name="dbhost" value="<?php echo $dbhost ?>"	 />
	  <input type="hidden" name="dbuser" value="<?php echo $dbuser ?>" />
	  <input type="hidden" name="dbpasswd" value="<?php echo $dbpasswd ?>" />
	  <input type="hidden" id="dbname" name="dbname" value="" />
	  <input type="hidden" name="dbprefix" value="<?php echo $dbprefix ?>" />
      
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" name="s1" type="submit" value="<?php echo $lang['next_step'] ?>" />
    </p>
	<script type="text/javascript">
	function g(id){
		return document.getElementById(id);
	}
	function chg_db(id){
		if(id==1){
			g('dbname1').disabled = false;
			g('dbname2').disabled = true;
		}else{
			g('dbname1').disabled = true;
			g('dbname2').disabled = false;
		}
	}
	function chksubmit(o){
		if(g('rd2').checked==true){
			if(o.dbname2.value ==0){
				alert("<?php echo $lang['dbname2_null'] ?>");
				o.dbname2.focus();
				return false;
			}else{
				o.dbname.value = o.dbname2.value;
			}
		}else{
			if(o.dbname1.value ==''){
				alert("<?php echo $lang['dbname1_null'] ?>");
				o.dbname1.focus();
				return false;
			}else{
				o.dbname.value = o.dbname1.value;
			}
		}
	}
	g('dbname2').disabled = true;
	</script>
<?php
	}
} 
elseif ($step == 5) 
{
	if(trim($_POST['dbname']) == "" || trim($_POST['dbhost']) == "" || trim($_POST['dbuser']) == "" || trim($_POST['dbname']) == "" || trim($_POST['dbprefix']) == "")
	{

?>
    <p><?php echo $lang['fill_all_blank'] ?></p>

    <p align="right">
      <input class="formbutton" type="button" value="<?php echo $lang['prev_step'] ?>" onclick="history.back(1)" />
    </p>
<?php
	} 
	else 
	{
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['admin_account'] ?></p>
<?php
		$dbhost = trim($_POST['dbhost']);
		$dbuser = trim($_POST['dbuser']);
		$dbpasswd = trim($_POST['dbpasswd']);
		$dbname = trim($_POST['dbname']);
		$dbprefix = trim($_POST['dbprefix']);

		$_l = mysql_connect($dbhost,$dbuser,$dbpasswd) or die($lang['could_not_connect']. mysql_error());
		if(!mysql_select_db($dbname,$_l))
		{
			mysql_query("create database `{$dbname}`;") or die($lang['invalid_query'] . mysql_error());
		}
		@mysql_close($_l);

		if(is_writable($config_file)) 
		{

			$dbhost = trim($_POST['dbhost']);
			$dbuser = trim($_POST['dbuser']);
			$dbpasswd = trim($_POST['dbpasswd']);
			$dbname = trim($_POST['dbname']);
            $dbprefix = trim($_POST['dbprefix']);
			if(empty($dbprefix))
			{
			$dbprefix ='scup_';
			}

			$str = "<?php".LF.LF;
			$str .= "// This is PHPSCUP auto-generated file. Do NOT modify me.".LF.LF;
			$str .= "define('DB_HOST','".$dbhost."');".LF.LF;
			$str .= "define('DB_USER','".$dbuser."');".LF.LF;
			$str .= "define('DB_PWD','".$dbpasswd."');".LF.LF;
			$str .= "define('DB_NAME','".$dbname."');".LF.LF;			
			$str .= "define('DB_TYPE','mysql');".LF.LF;
			$str .= "define('DB_CHARSET','".$charset."');".LF.LF;			
			$str .= "define('DB_PREFIX','".$dbprefix."');".LF.LF;
			$str .= "define('DB_PCONNECT',0);".LF.LF;			
			$str .= "?>".LF;
			
			$fp = fopen($config_file,'w'); 
			if (!$fp) 
			{ 
				exit("Can not open file <b>$config_file</b> .");
			} 
			if(is_writable($config_file))
			{
				if(@fwrite($fp,$str))
				{
					$msg .= "<font color=blue>{$config_file} ".$lang['write_success']."</font>";
				}else
				{
					$msg .= "<font color=red>{$config_file} ".$lang['write_failture']."</font>";
					$flag = true;
				}
				@fclose($fp);
			}else
			{
				$msg .= "<font color=red>{$config_file} ".$lang['cant_write']."</font>";
				$flag = true;
			}
			echo $msg;
			if($flag)
			{
				exit;
			}
		}
		
		require_once $config_file;
		require_once  ROOT_PATH.'/install/sql/mysql.lib.php';
		$db = new SCup_mysql(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PREFIX,DB_PCONNECT,DB_CHARSET);

		$db->connect();

		$tpf = DB_PREFIX;

		$msg = '';
		$error = false;
		$curr_os = PHP_OS;
		$curr_php_version = PHP_VERSION;
		if($curr_php_version < '4.0.6') 
		{
			$msg .= "<font color=\"#FF0000\">".$lang['php_version_too_lower']."</font><br />";
			$error = true;
		}

		$query = $db->query("SELECT VERSION()");
		$curr_mysql_version = $db->result($query, 0);
		
		if($curr_mysql_version < '4.1') 
		{
			$msg .= "<font color=\"#FF0000\">".$lang['mysql_version_too_lower']."</font><br />";
			$error = true;
		}

		$db->select_db(DB_NAME);
		if($db->error()) 
		{
			if($db->error()) 
			{
				$msg .= "<font color=\"#FF0000\">".$lang['database_not_found']."</font><br />";
				$error = true;
			} else 
			{
				$db->select_db(DB_NAME);
				$msg .= $lang['database_create_success']."<br />";
			}
		}
		
		//$db->query("SELECT COUNT(*) FROM {$tpf}users ", 'SILENT');
		if(!$db->error()) 
		{
			$msg .= "<font color=\"#FF0000\">".$lang['database_already_exists']."</font><br />";
			$alert = " onclick=\"return confirm('".$lang['database_confirm']."');\"";
		} else 
		{
			$alert = '';
		}

		if($error) 
		{
			$msg .= "<font color=\"#FF0000\">".$lang['file_not_chmod']."</font>";
		} else 
		{
			$msg .= $lang['server_env_ok'];
		}
		if ($msg) 
		{
			echo "<p>".$msg."</p>";
		}
		if($error) 
		{
?>
    <p align="right">
      <input type="button" class="formbutton" value="<?php echo $lang['quit'] ?>" onclick="javascript: window.close();">
    </p>
<?php
		} 
		else 
		{
?>
    <p>&nbsp;</p>
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
      <tr>
        <td width="30%" nowrap><?php echo $lang['username'] ?>:</td>
        <td><input type="text" value="" name="username" class="formfield" style="width:150px"></td>
      </tr>
      <tr>
        <td width="30%" nowrap><?php echo $lang['password'] ?>:</td>
        <td><input type="password" value="" name="password" class="formfield" style="width:150px"></td>
      </tr>
      <tr>
        <td width="30%" nowrap><?php echo $lang['confirm_password'] ?>:</td>
        <td><input type="password" value="" name="confirmpassword" class="formfield" style="width:150px"></td>
      </tr>
      <tr>
        <td width="30%" nowrap><?php echo $lang['email'] ?>:</td>
        <td><input type="text" value="" name="email" class="formfield" style="width:150px"></td>
      </tr>
    </table>
	<script language="JavaScript" type="text/javascript">
	document.myform.username.focus();
	function chksubmit(o){
		if(o.username.value == ""){
			alert("<?php echo $lang['js_username'] ?>");
			o.username.focus();
			return false;
		}
		if(o.password.value == ""){
			alert("<?php echo $lang['js_password'] ?>");
			o.password.focus();
			return false;
		}
		if(o.password.value.length <6){
			alert("<?php echo $lang['js_password_too_short'] ?>");
			o.password.focus();
			return false;
		}
		if(o.password.value != o.confirmpassword.value){
			alert("<?php echo $lang['js_confirm_password'] ?>");
			o.confirmpassword.focus();
			return false;
		}
		if(o.email.value == "" || o.email.value.indexOf('@') ==-1 || o.email.value.indexOf('.') ==-1){
			alert("<?php echo $lang['js_email'] ?>");
			o.email.focus();
			return false;
		}
		o.s1.disabled = true;	
		return true;
	}
	</script>
    <p>&nbsp;</p>

    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" name="s1" type="submit" value="<?php echo $lang['next_step'] ?>" <?php echo $alert;?> />
    </p>
<?php
		}
	}
} 
elseif ($step == 6) 
{
	$username = addslashes(trim($_POST['username']));
	$password = $_POST['password'];
	$confirmpassword = $_POST['confirmpassword'];
	$email = trim($_POST['email']);
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['check_user_info'] ?></p>
<?php
    if ($username == "" || $password == "" || $confirmpassword == "" || $email == '') 
	{
		$msg = "<p>".$lang['fill_all_blank']."</p>";
		$error = true;
    } elseif (strlen($_POST['password']) < 6) 
	{
		$msg = "<p>".$lang['s_password_too_short']."</p>";
		$error = true;
	} elseif ($password != $confirmpassword) 
	{
		$msg = "<p>".$lang['s_password_not_same']."</p>";
		$error = true;
	} elseif (strlen($email) <6 || strpos($email,'@') ===false)
	{
		$msg = "<p>".$lang['s_email']."</p>";
		$error = false;
    } else 
	{
		$msg = "<p>".$lang['s_check_server_env']."</p>";
		$error = false;
	}
	$name_key = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'#','$','(',')','%','@','+','?',';','^');
	foreach($name_key as $value)
	{
		if (strpos($username,$value) !== false)
		{ 
			$msg = "<p>".$lang['bad_chars']."</p>";
			$error = true;
		}
	}
	if ($error) 
	{
		echo $msg;
?>

    <p align="right">
      <input class="formbutton" type="button" value="<?php echo $lang['prev_step'] ?>" onclick="history.back(1)" />
    </p>
<?php
	} 
	else 
	{
		echo $msg;
?>
    <p>&nbsp;</p>
    <p><?php echo $lang['username'] ?>: <?php echo $username;?><input type="hidden" name="username" value="<?php echo $username;?>" /></p>
    <p><?php echo $lang['password'] ?>: <?php echo $password;?><input type="hidden" name="password" value="<?php echo $password;?>" /></p>
    <p><?php echo $lang['email'] ?>: <?php echo $email;?><input type="hidden" name="email" value="<?php echo $email;?>" /></p>
    <p>&nbsp;</p>
    <p><?php echo $lang['user_info_right'] ?></p>

    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" type="submit" value="<?php echo $lang['next_step'] ?>" />
    </p>
<?php
	}
} 
elseif ($step == 7) 
{
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$email = trim($_POST['email']);
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['import_data'] ?></p>
	<p>
<?php
	require_once $config_file;

	require_once  ROOT_PATH.'/install/sql/mysql.lib.php';

	$db = new SCup_mysql(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PREFIX,DB_PCONNECT,DB_CHARSET);

	$db->connect();

	$tpf = DB_PREFIX;


	$sql = file_get_contents($sqlfile);
    $sql=stripslashes($sql);
	if($pre!='scup_')
	{
	  $sql=str_replace("scup_",DB_PREFIX,$sql);
	}		
	
	
	sql_execute($sql);
	
	$ins = array(
		'user_name' => $db->escape($username),
		'user_passwd' => md5($password),
		'user_email' => $db->escape($email),
		'role_id' => 'admin:2|scup:2',
		'reg_time' => time(),
		'last_ip' => $db->escape($_SERVER['REMOTE_ADDR']),
		'config'=>'',
	);
	$db->query("insert into {$tpf}users set ".sql_array($ins).";");
?>
    </p>
    <p><?php echo $lang['all_create'] ?><?php echo $tablenum;?> <?php echo $lang['tables'] ?>.</p>

    <p><?php echo $lang['base_settings'] ?></p>
	<?php 
	$arr = explode('install',$_SERVER['REQUEST_URI']);
	$phpscup_url = 'http://'.$_SERVER['HTTP_HOST'].$arr[0];
	
	$phpscup_url = substr($phpscup_url,0,-1);
	?>
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
      <tr>
        <td width="15%" nowrap><?php echo $lang['site_title'] ?>:</td>
        <td><input type="text" value="<?php echo $lang['project_name'] ?>" name="site_title" class="formfield" style="width:200px" maxlength="50"></td>
      </tr>
      <tr>
        <td width="15%" nowrap><?php echo $lang['phpscup_url'] ?>:</td>
        <td><input type="text" value="<?php echo $phpscup_url; ?>" name="phpscup_url" class="formfield" style="width:300px" maxlength="50"> <?php echo $lang['phpscup_url_tips'] ?></td>
      </tr>
      <tr>
        <td width="15%" nowrap><?php echo $lang['encrypt_key'] ?>:</td>
        <td><input type="text" id="encrypt_key" name="encrypt_key" value="" class="formfield" maxlength="16"/>&nbsp;<a href="###" onclick="make_key();"><?php echo $lang['reflash_key'] ?></a></td>
      </tr>
      <tr>
        <td width="15%" nowrap><?php echo $lang['open_gzip'] ?>:</td>
        <td><input type="radio" name="gzipcompress" value="1"/><?php echo $lang['yes'] ?>
		<input type="radio" name="gzipcompress" value="0" checked="checked"  /><?php echo $lang['no'] ?></td>
      </tr>
	</table>  
    <input type="hidden" name="username" value="<?php echo $username;?>" />
    <input type="hidden" name="password" value="<?php echo $password;?>" />
	<script language="javascript">
	make_key();
	function chksubmit(o){
		if(o.site_title.value == ""){
			alert("<?php echo $lang['js_site_title'] ?>");
			o.site_title.focus();
			return false;
		}
		if(o.phpscup_url.value == ""){
			alert("<?php echo $lang['js_phpscup_url'] ?>");
			o.phpscup_url.focus();
			return false;
		}
		if(o.encrypt_key.value == ""){
			alert("<?php echo $lang['js_encrypt_key'] ?>");
			o.encrypt_key.focus();
			return false;
		}
	}
	function make_key(){
	   var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	   var tmp = "";
	   var code = "";
	   for(var i=0;i<12;i++){
		   code += chars.charAt(Math.ceil(Math.random()*100000000)%chars.length);
	   }
	   document.getElementById('encrypt_key').value = code;
	}
	</script>
    <p align="right">
      <input type="hidden" name="step" value="<?php echo $step+1 ?>" />
      <input class="formbutton" type="submit" value="<?php echo $lang['next_step'] ?>" />
    </p>
<?php 
}elseif($step ==8){
	require_once $config_file;
	require_once  ROOT_PATH.'/install/sql/mysql.lib.php';

		$db = new SCup_mysql(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PREFIX,DB_PCONNECT,DB_CHARSET);

		$db->connect();

		$tpf = DB_PREFIX;

	$site_title = trim($_POST['site_title']);
	$phpscup_url = trim($_POST['phpscup_url']);
	$gzipcompress = trim($_POST['gzipcompress']);
	$encrypt_key = trim($_POST['encrypt_key']);
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	
    if ($site_title == "" || $phpscup_url == "" || $encrypt_key == "") 
	{
		$msg = "<p>".$lang['site_title_and_phpscup_url']."</p>";
		$error = true;
	}
	
	if ($error) 
	{
		echo $msg;
?>

    <p align="right">
      <input class="formbutton" type="button" value="<?php echo $lang['prev_step'] ?>" onclick="javascript:history.back();" />
    </p>
<?php
	} 
	else 
	{
?>
    <p class="title"><?php echo $lang['the'] ?>(<span class="txtblue"><?php echo $step ?></span>/<?php echo $total_step ?>)<?php echo $lang['step'] ?>:<?php echo $lang['finished_install'] ?></p>
	<p>
<?php	
		$settings = array(
			'site_title' => $db->escape($site_title),
			'site_url' => $db->escape($phpscup_url),
			'enabled_gzip' => (int)$gzipcompress,
			'encrypt_key' => $db->escape($encrypt_key),

		);
		$setting_file = ROOT_PATH.'/data/config.inc.php';
		$str = $str2 = '';
		foreach($settings as $k => $v){
			$str2 .= "('$k','$v'),";
		}
		$str2 = substr($str2,0,-1);
		$db->query("replace into {$tpf}settings (keyname,value) values $str2 ;");

		
		$q = $db->query("select * from {$tpf}settings");
		while($rs = $db->fetch_array($q)){
			$str_c .= "\t define('".strtoupper($rs['keyname'])."','".$rs['value']."');".LF;
		}
		$db->free_result($q);
		unset($rs);
		
		$str = "<?php".LF.LF;
		$str .= "if(!defined('IN_PHPSCUP'))exit('hacking'); // This is PHPSCUP auto-generated file. Do NOT modify me.".LF;
		$str .= "// Cache Time: ".date("Y-m-d H:i:s").LF.LF;

		$str .= $str_c.LF.LF;

		$str .= "?>".LF;
		
		$msg = '';
		$fp = fopen($setting_file,'w'); 
		if (!$fp) { 
			exit("Can not open file <b>$setting_file</b> .");
		} 
		if(is_writable($setting_file)){
			if(@fwrite($fp,$str)){
				$msg .= "<font color=blue>{$setting_file} {$lang['write_success']}</font>";
			}else{
				$msg .= "<font color=red>{$setting_file} {$lang['write_fail']}</font>";
				$flag = true;
			}
			@fclose($fp);
		}else{
			$msg .= "<font color=red>{$setting_file} {$lang['cannot_write']}</font>";
			$flag = true;
		}
		write_file(ROOT_PATH.'/data/install.lock','PHPSCUP installed! Time:'.date("Y-m-d H:i:s"));
		echo $msg;
		if($flag){
			exit;
		}
?>

    <p><?php echo $lang['finished_tips'] ?></p>
	<p class="txtblue"><?php echo $lang['reinstall_tips'] ?></p>
    <p><?php echo $lang['thanks'] ?></p>
    <p>&nbsp;</p>
    <p><?php echo $lang['username'] ?>: <?php echo $username;?></p>
    <p><?php echo $lang['password'] ?>: <?php echo $password;?></p>
    <p>&nbsp;</p>
    <p><a href="../"><?php echo $lang['visit_phpscup'] ?></a></p>
    <p><a href="../admin"><?php echo $lang['visit_admin_phpscup'] ?></a></p>
    <p align="right"><a href="http://www.phpscup.com/" target="_blank"><?php echo $lang['phpscup_official_site'] ?></a></p>
<?php
	}
}
?>
  </form>
</div>
<strong>Powered by <a href="http://www.phpscup.com/" target="_blank">PHPSCUP Team</a> <?php echo PHPSCUP_VERSION;?> (C) 2008-<?php echo NOW_YEAR ?> All Rights Reserved.</strong>
</body>
</html>
<?php

function sql_execute($sql){
	global $db;	
    $sqls = sql_split($sql);
	if(is_array($sqls)){
		foreach($sqls as $sql){
			if(trim($sql) != ''){
				$db->query($sql);
			}
		}
	}else{
		$db->query($sqls);
	}
	return true;
}

function sql_split($sql){
	global $db;	
	if($db->version() > '4.1'){
		$sql = preg_replace("/TYPE=(InnoDB|MyISAM)( DEFAULT CHARSET=[^; ]+)?/", "TYPE=\\1 DEFAULT CHARSET=utf8",$sql);
	}
	$sql = str_replace("\r", "\n", $sql);
	$ret = array();
	$num = 0;
	$queriesarray = explode(";\n", trim($sql));
	unset($sql);
	
	foreach($queriesarray as $query){
		$ret[$num] = '';
		$queries = explode("\n", trim($query));
		$queries = array_filter($queries);
		
		foreach($queries as $query){
			$str1 = substr($query, 0, 1);
			if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
		}
		$num++;
	}
	return($ret);
}



    function sql_array ($arr){
		$ins = array();
		reset($arr);
		while(list($c, $v) = each($arr)){
			$ins[] = ($v === NULL ? sprintf('`%s`=NULL', $c) : sprintf('`%s`=\'%s\'', $c, $v));
		}
		return implode(', ', $ins);
    }



function output_txt($output_txt = 1, $output = 1) {
	if($output_txt) {
		$text = '... <font color="#0000EE">Yes</font><br />';
		if(!$output) {
			return $text;
		}
		echo $text;
	} else {
		$text = '... <font color="#FF0000">No</font><br />';
		if(!$output) {
			return $text;
		}
		echo $text;
	}
}


function write_file($f,$str,$mode = 'wb') { 
    $fp = fopen($f,$mode); 
    if (!$fp) { 
        exit("Can not open file <b>$f</b> .code:1");
    } 
	if(is_writable($f)){
		if(!fwrite($fp,$str)){
			exit("Can not write file <b>$f</b> .code:2");
		} 
	}else{
		exit("Can not write file <b>$f</b> .code:3");
	}
    fclose($fp); 
} 
function read_file($f) { 
    if (file_exists($f)) { 
        if (PHP_VERSION >= "4.3.0") return file_get_contents($f); 
        $fp = fopen($f,"r"); 
        $fsize = filesize($f); 
        $c = fread($fp, $fsize); 
        fclose($fp); 
        return $c; 
    } else{
		exit("<b>$f</b> does not exist!"); 
	}
} 

?>